package com.sensetime.mycamera;

import android.util.Log;

import java.io.File;
import java.io.FileOutputStream;
import java.io.FileWriter;
import java.io.IOException;

public class FileUtils {
    static String TAG = "FileUtils";
    public static void write(byte[] data) {
        FileOutputStream fos = null;
        try {
            File file = new File(MyApp.instance.getExternalCacheDir(), "out.h264");
            if (!file.getParentFile().exists()) {
                file.getParentFile().mkdirs();
            }
            if (!file.exists()) {
                file.createNewFile();
            }
            Log.e(TAG, "path="+file.getAbsolutePath());
            fos = new FileOutputStream(file, true);
            fos.write(data);
            fos.write('\n');
        } catch (Exception e) {
            e.printStackTrace();
            ;
        } finally {
            if (fos != null) {
                try {
                    fos.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        }
    }

    public static void writeContent(byte[] data) {
        char[] HEX_CHARS = {
                '0', '1', '2', '3', '4', '5', '6', '7', '8', '9', 'A', 'B', 'C', 'D', 'E', 'F'
        };
        StringBuffer sb = new StringBuffer();
        for (byte b : data) {
            sb.append(HEX_CHARS[(b & 0xf0) >> 4] );
            sb.append(HEX_CHARS[b & 0x0f]);
        }
        Log.e(TAG, "writeContent:"+sb.toString());
        FileWriter fos = null;
        try {
            File file = new File(MyApp.instance.getExternalCacheDir(), "out2.h264");
            if (!file.getParentFile().exists()) {
                file.getParentFile().mkdirs();
            }
            if (!file.exists()) {
                file.createNewFile();
            }
            Log.e(TAG, "path2="+file.getAbsolutePath());
            fos = new FileWriter(file, true);
            fos.write(sb.toString());
            fos.write('\n');
        } catch (Exception e) {
            e.printStackTrace();

        } finally {
            if (fos != null) {
                try {
                    fos.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        }
    }
}
